﻿using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Web.UI.WebControls;
using InventoryManagement;
using System.Data;

namespace MII_FIF.Layouts.MII_FIF.ChangeRequest
{
    public partial class ViewCRStorage : LayoutsPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Generic.IsUserInGroup(Configuration.ChangeRequestGroupName))
            {
                btnAdd.Click += btnAdd_Click;
                gridCRStorage.RowCommand += gridCRStorage_RowCommand;
                gridCRStorage.PageIndexChanging += gridCRStorage_PageIndexChanging;
                //((Label)Header.FindControl("lblHeader")).Text = "View Change Request Storage";
                if (!IsPostBack)
                {
                    load();
                }
            }
            else
            {
                Response.Redirect(Generic.GenLink("/"));
            }
        }

        void gridCRStorage_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gridCRStorage.DataSource = (DataTable)ViewState["CR"];
            gridCRStorage.PageIndex = e.NewPageIndex;
            gridCRStorage.DataBind();
        }

        void gridCRStorage_RowCommand(object sender, System.Web.UI.WebControls.GridViewCommandEventArgs e)
        {
            if (e.CommandName.ToLower() == "view")
            {
                Response.Redirect(Generic.GenLink("/_Layouts/MII_FIF/ChangeRequest/CRStorage.aspx?ID=" + e.CommandArgument));
            }
        }

        void btnAdd_Click(object sender, EventArgs e)
        {
            Response.Redirect(Generic.GenLink("/_Layouts/MII_FIF/ChangeRequest/CRStorage.aspx"));
        }

        void load()
        {
            CR_Storage CR = new CR_Storage();
            ViewState["CR"] = CR.SelectByParam();
            //DataTable DT = (DataTable)ViewState["CR"];
            //DT.Columns.Add("LabelName", typeof(string));

            //if (DT.Rows.Count > 0)
            //{
            //    for (int i = 0; i < DT.Rows.Count; i++)
            //    {
            //        Inventory_Storage IM = new Inventory_Storage();
            //        IM.ID = Guid.Parse(DT.Rows[i]["ID_Storage"].ToString());
            //        IM.Select();
            //        DT.Rows[i]["LabelName"] = IM.LabelName;
            //        DT.AcceptChanges();
            //    }
            //}
            //ViewState["CR"] = DT;
            gridCRStorage.DataSource = (DataTable)ViewState["CR"];
            gridCRStorage.DataBind();
        }
    }
}
